﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BudgetMealsModels.Abstract;
using BudgetMealsModels.Entities;
using BudgetMealsModels.Concrete;
using System.Threading;

namespace BudgetMealsUI.Controllers
{
    public class CommentController : Controller
    {
        private ICommentRepository repository;
        private IRecipeRepository recipeRepository;
        private IUserRepository _userRepo;

        public CommentController(ICommentRepository repo, IRecipeRepository reciRepo, IUserRepository userRepo)
        {
            repository = repo;
            recipeRepository = reciRepo;
            _userRepo = userRepo;
        }

        public ActionResult Create(int id, int recipeId)
        {
            Recipe recipe = recipeRepository.Recipes.FirstOrDefault(r => r.RecipeID == recipeId);
            Comment comment = repository.Comments.FirstOrDefault(x => x.CommentID == id);
            if (comment == null)
            {
                comment = new Comment();
            }
            comment.UserCreated = (String)Session["CurrentUserName"];
            comment.Recipe = recipe;
            return View(comment);
        }

        public ActionResult AddComment(int recipeId, int commentId, int parentId)
        {
            Comment newComment = repository.Comments.FirstOrDefault(x=> x.CommentID == commentId);
            Comment parent = repository.Comments.FirstOrDefault(x => x.CommentID == parentId);
            if (newComment == null)
            {
                newComment = new Comment();
            }
            newComment.Recipe = parent.Recipe;
            newComment.UserCreated = (String)Session["CurrentUserName"];
            newComment.UserCreatedID = (int)Session["CurrentUser"];
            newComment.ParentID = parentId;
            return View(newComment);
        }

        /*public ActionResult AddReply(Comment comment)
        {
            Comment parent = repository.Comments.FirstOrDefault(x => x.CommentID == comment.ParentID);
            comment.Recipe = parent.Recipe;
            if (comment.Review != null)
            {
                parent.HierarchicalComments.Add(comment);
                recipeRepository.AddComment(comment);
            }
            return RedirectToAction("ShowRecipe", "Recipe", new { Id= comment.Recipe.RecipeID });
        }*/

        public ActionResult AddReply(int parentID, string review)
        {
            if (!String.IsNullOrEmpty(review))
            {
                int userID = (int)Session["CurrentUser"];
                User user = _userRepo.Users.FirstOrDefault(u => u.UserID == userID);
                Comment parent = repository.Comments.FirstOrDefault(x => x.CommentID == parentID);
                var comment = new Comment { ParentID = parentID, Recipe = parent.Recipe, Review = review, UserCreated = user.UserName, UserCreatedID = user.UserID };
                comment.Recipe = parent.Recipe;
                if (comment.Review != null)
                {
                    parent.HierarchicalComments.Add(comment);
                    recipeRepository.AddComment(comment);
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "You must enter a reply.");
                }
                return PartialView("ShowReply", comment);
            }
            return PartialView("ShowReply", null);

            //return RedirectToAction("ShowRecipe", "Recipe", new { Id = comment.Recipe.RecipeID });
        }

        public ActionResult AddReview(int recipeId, int rating, string review)
        {
            int userID = (int)Session["CurrentUser"];
            User user = _userRepo.Users.FirstOrDefault(u => u.UserID == userID);
            Recipe recipe = recipeRepository.Recipes.FirstOrDefault(r => r.RecipeID == recipeId);
            var comment = new Comment { ParentID = 0, Recipe = recipe, Review = review, UserCreated = user.UserName, UserCreatedID = user.UserID, RecipeRating = rating};
            comment.Recipe = recipe;
            recipeRepository.AddComment(comment);
            comment = repository.Comments.FirstOrDefault(x => ((x.Review == null && comment.Review == null) || x.Review.Equals(comment.Review)) && x.UserCreatedID == comment.UserCreatedID && x.ParentID == comment.ParentID && x.RecipeRating == comment.RecipeRating);
            if(!String.IsNullOrEmpty(review))
                return PartialView("ShowReview", comment);
            return PartialView("ShowReview", null);
        }

        //
        // GET: /Comment/Delete/5
 
        //public ActionResult Delete(int id, String stringURL)
        //{
        //    Comment comment = repository.Comments.FirstOrDefault(x => x.CommentID == id);
        //    if (comment != null)
        //        repository.Delete(comment);
        //    return Redirect(stringURL);
        //}

        public ActionResult Delete(int id)
        {
            Comment comment = repository.Comments.FirstOrDefault(x => x.CommentID == id);
            Recipe recipe = comment.Recipe;
            if (comment != null)
                repository.Delete(comment);
            return PartialView("ShowReply", comment);
        }

        public ActionResult ShowReply(Comment comment)
        {
            return PartialView(comment);
        }
        [HttpGet]
        public JsonResult Test(int RecipeID)
        {
            var result = repository.Comments.Where(c => c.Recipe.RecipeID == RecipeID);

            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
